<template>
  <div>
    <el-page-header @back="goBack" content="添加歌曲">
    </el-page-header>
    <el-card>
      <el-row>
        <el-steps :active="active" simple style="margin: 15px 0;">
          <el-step title="基本信息" icon="el-icon-edit"></el-step>
          <el-step title="添加歌手" icon="el-icon-user"></el-step>
          <el-step title="添加类型" icon="el-icon-picture"></el-step>
        </el-steps>
      </el-row>
      <el-row v-if="active === 1">
        <el-col :span="8" class="add-col">
          <el-form ref="form" :model="song" label-width="80px">
            <el-form-item label="歌曲名称">
              <el-input v-model="song.name"></el-input>
            </el-form-item>
            <el-form-item label="所属专辑">
              <el-select v-model="song.parentId" placeholder="请选择歌曲所属专辑" style="width: 335px;">
                <el-option v-for="item in specials" :label="item.name" :value="item.id" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="歌曲评分">
              <el-input-number style="width: 335px;" v-model="song.grade" :min="1" :max="100"></el-input-number>
            </el-form-item>
            <el-form-item label="歌曲热度">
              <el-input-number style="width: 335px;" v-model="song.hot" :min="1" :max="100"></el-input-number>
            </el-form-item>
            <el-form-item label="歌词">
              <el-input type="textarea" v-model="song.libretto" placeholder="使用中文，分隔"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row v-else-if="active === 2">
        <el-transfer style="text-align: left; display: inline-block"
                     v-model="checkSingers" :data="singersdata" :titles="['待选歌手', '已选歌手']">
        </el-transfer>
      </el-row>
      <el-row v-else>
        <el-tree :data="types" :props="props" node-key="id" show-checkbox default-expand-all @check="checkChange">
        </el-tree>
      </el-row>
      <el-row style="margin-top: 15px;">
        <el-button @click="lastStep" :style="active === 1 ? 'display: none':'display: inline-block'">上一步</el-button>
        <el-button @click="nextStep">{{active >= 3 ? '添加' : '下一步'}}</el-button>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      active: 1,
      song: {
        name: '',
        parentId: '',
        grade: 60,
        fileType: '',
        hot: 50,
        libretto: ''
      },
      // 选中的歌手
      checkSingers: [],
      // 专辑集合
      specials: [],
      // 类型集合
      types: [],
      // 所有歌手集合
      singers: [],
      // 歌手显示数据
      singersdata: [],
      // 歌曲类型结构
      props: {
        label: 'name',
        children: 'child'
      },
      // 歌曲类型列表
      checkTypes: [],
      // 添加之后的歌曲id
      addId: -1
    }
  },
  methods: {
    // 类型选中发生改变时
    checkChange (node, data) {
      this.checkTypes = [...data.checkedKeys, ...data.halfCheckedKeys]
    },
    // 点击下一步
    nextStep () {
      this.active++
      if (this.active === 4) {
        this.$http.post('song', this.song).then(res => {
          if (res.data.code === 10000) {
            this.addId = res.data.data.id
            if (this.checkTypes.length > 0) {
              this.checkTypes = this.checkTypes.map(Number)
              this.$http.post(`type/song/${this.addId}`, this.checkTypes)
            }
            if (this.checkSingers.length > 0) {
              this.checkSingers = this.checkSingers.map(Number)
              this.$http.post(`singer/song/${this.addId}`, this.checkSingers)
            }
            this.$message.success({
              message: '添加成功',
              duration: 1000
            })
            this.$router.push('/songlist')
          }
        })
      }
    },
    // 点击上一步
    lastStep () {
      this.active--
    },
    // 返回
    goBack () {
      this.$router.go(-1)
    },
    // 迭代遍历歌手
    iterSinger (singers) {
      singers.forEach(item => {
        this.singersdata.push({
          label: item.name,
          key: item.id
        })
      })
    },
    // 查询所有歌手
    getSingers () {
      this.$http.get('singer').then(res => {
        this.singers = res.data.data
        this.iterSinger(this.singers)
      })
    },
    // 查询所有专辑
    getSpecials () {
      this.$http.get('song/special').then(res => {
        this.specials = res.data.data
      })
    },
    // 查询所有类型
    getTypes () {
      this.$http.get('type').then(res => {
        this.types = res.data.data
      })
    }
  },
  created () {
    this.getSingers()
    this.getSpecials()
    this.getTypes()
  }
}
</script>

<style scoped>
  .add-col{
    margin-left: 300px;
  }
</style>
